package silo.dungeonbuddy;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This class is where the game starts and it is the container of the game loop.
 */

public class Start extends Canvas
{
	private BufferStrategy strategy;/* The strategy that allows us to use accelerate page flipping */
	private boolean gameRunning = true;/* True if the game is currently "running", i.e. the game loop is looping */
	private boolean gameOver = false;
	private boolean pause = false;
	private Random generator;
	private boolean setup = true;
	private boolean server = false;
	private boolean client = false;
	private String serverIP;
	private int width;
	private int height;
	private int backColorH;
	private int backColorS;
	private int backColorB;
	private IntValue offsetX = new IntValue(0);
	private IntValue offsetY = new IntValue(0);
	private int squaresPerScreen;
	private Square selectedSquare;
	private TextBox selectedTextBox=null;
	private FlatImage selectedPiece=null;
	private FlatImage previouslySelectedPiece=null;
	private ScrollBar selectedScrollBar=null;
	private Unit myUnit=null;
	private Unit turnUnit=null;
	private Vector turnList = new Vector();
	private String backName;
	private Vector effectList = new Vector();
	private Vector objectList = new Vector();
	private Vector markerList = new Vector();
	private Vector terrainList = new Vector();
	private Vector unitList = new Vector();/* The list of all the entities that exist in our game */
	//private Vector flatImageList = new Vector();
	private Vector removeList = new Vector();/* The list of entities that need to be removed from the game this loop */
	private Vector clientList = new Vector();
	private Board gameBoard;
	private Server listener;
	private Client talker;
	private long lastCommTime;
	private String userName="";
	//KEYS  v v
	private boolean kUp = false;
	private boolean kDown = false;
	private boolean kLeft = false;
	private boolean kRight = false;
	private boolean kA = false;
	private boolean kB = false;
	private boolean kC = false;
	private boolean kD = false;
	private boolean kE = false;
	private boolean kF = false;
	private boolean kG = false;
	private boolean kH = false;
	private boolean kI = false;
	private boolean kJ = false;
	private boolean kK = false;
	private boolean kL = false;
	private boolean kM = false;
	private boolean kN = false;
	private boolean kO = false;
	private boolean kP = false;
	private boolean kQ = false;
	private boolean kR = false;
	private boolean kS = false;
	private boolean kT = false;
	private boolean kU = false;
	private boolean kV = false;
	private boolean kW = false;
	private boolean kX = false;
	private boolean kY = false;
	private boolean kZ = false;
	private boolean k0 = false;
	private boolean k1 = false;
	private boolean k2 = false;
	private boolean k3 = false;
	private boolean k4 = false;
	private boolean k5 = false;
	private boolean k6 = false;
	private boolean k7 = false;
	private boolean k8 = false;
	private boolean k9 = false;
	private boolean kDelete = false;
	private boolean kBackspace = false;
	private boolean kPeriod = false;
	private boolean kSpace = false;
	private boolean kEnter = false;
	private boolean kShift = false;
	private boolean kControl = false;
	//KEYS  ^ ^
	//MOUSE v v
	private int mouseX;
	private int mouseY;
	private boolean mouseLeft = false;
	private boolean mouseRight = false;
	private boolean doubleClick = false;
	private long clickTime=0;
	private long mouseUpdateTime=System.currentTimeMillis();
	private int firstClickX;
	private int firstClickY;
	private boolean sizingPiece = false;
	private boolean movingPiece = false;
	private boolean scrolling = false;
	//MOUSE ^ ^
	//GUI OBJECTS v v
	private Vector formList = new Vector();
	private Vector textBoxList = new Vector();
	private Vector buttonList = new Vector();
	private Vector scrollBarList = new Vector();
	private Vector labelList = new Vector();
	private ToolBox toolBox;
	private CheckBox showGridCheckBox;
	//      private ArrayList radioButtonList = new ArrayList();
	//GUI OBJECTS ^ ^
	
	public Start()
	{
		//INIT VARIABLES
		backName="sprites/defaultBack.png";
		squaresPerScreen=20;
		selectedSquare=null;
		backColorH=0;
		backColorS=0;
		backColorB=100;
		lastCommTime = System.currentTimeMillis();
		textBoxList = new Vector();
		gameRunning = true;
		gameOver = false;
		pause = false;
		width=800;
		height=600;
		offsetX.setValue(0);
		offsetY.setValue(0);
		clickTime=System.currentTimeMillis();
		generator = new Random();
		
		// create a frame to contain the game
		JFrame container = new JFrame("Dungeon Buddy 2.0");

		// get hold the content of the frame and set up the resolution of the game
		JPanel panel = (JPanel)container.getContentPane();
		panel.setPreferredSize(new Dimension(width, height));
		panel.setLayout(null);

		// setup canvas size and put it into the content of the frame
		setBounds(0, 0, width, height);
		container.setLocation(310, 10);
		panel.add(this);

		// Tell AWT not to repaint the canvas since this will be done
		// in accelerated mode
		setIgnoreRepaint(true);

		// make the window visible 
		container.pack();
		container.setResizable(true);
		
		BufferedImage image = null;
		try
		{
			image = ImageIO.read(container.getClass().getResource("/sprites/icon.jpg"));
		}catch (IOException e)
		{
			e.printStackTrace();
		}
		container.setIconImage(image);
		
		container.setVisible(true);

		// add a listener to respond to the user closing the window. If they
		// do - exit the game
		container.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		
		// add a key input class (defined below) to canvas
		// to respond to key presses
		addKeyListener(new KeyInputHandler());
		//also mouse input class:
		addMouseListener(new MouseInputHandler());
		
		// GET FOCUS
		requestFocus();

		// CREATE BUFFER STRATEGY SO AWT WILL HANDLE ACCELERATED
		// GRAPHICS.
		createBufferStrategy(2);
		strategy = getBufferStrategy();
		
		container.addComponentListener(new ComponentAdapter()
		{
			public void componentResized(ComponentEvent e)
			{
				createBufferStrategy(2);
				strategy = getBufferStrategy();
				width=((Component)e.getSource()).getSize().width-10;
				height=((Component)e.getSource()).getSize().height-35;
				setBounds(0, 0, width, height);
				Graphics2D g2 = (Graphics2D) strategy.getDrawGraphics();
				resizeMainWindow(g2);
			}
		});
		
		// GET CONTEXT FOR ACCELERATED GRAPHICS
		Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
		
		// THE NEXT SECTION SETS UP THE STARTUP SCREEN
		for(int i=0;i<10;i++)
		{
			textBoxList.add(new TextBox("",70,70+(30*i),g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
		}
		textBoxList.add(new TextBox("",(int)Math.floor(width/2)+20+g.getFontMetrics().stringWidth("Enter server's IP address:")+10,35,g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
		textBoxList.add(new TextBox("",(int)Math.floor(width/2)+20+g.getFontMetrics().stringWidth("Enter server's IP address:")+10,65,g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
		buttonList.add(new Button(this,(int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-10-20-20-10-g.getFontMetrics().stringWidth("Start server")),height-60,true,0,"Start server",Color.getHSBColor(57, 52, 84),g,true));
		buttonList.add(new Button(this,(int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-10-20),height-60,true,0,"Start client",Color.getHSBColor(57, 52, 84),g,true));
	}//Constructor
	
	private void gameloop()
	{
		long lastLoopTime = System.currentTimeMillis();
		
		// LOOP 'TIL GAME ENDS (GAME LOOP)
		while (gameRunning)
		{
			//CHECK HOW LONG THE LAST LOOP TOOK.  This is important for
			// controlling the speed of any animations that I may want to
			// add in later.
			long delta = System.currentTimeMillis() - lastLoopTime;
			lastLoopTime = System.currentTimeMillis();
			
			// GET CONTEXT FOR ACCELERATED GRAPHICS
			Graphics2D g;
			try
			{
				g = (Graphics2D) strategy.getDrawGraphics();
			}catch(Exception e)
			{
				createBufferStrategy(2);
				strategy = getBufferStrategy();
				g = (Graphics2D) strategy.getDrawGraphics();
			}
			
			// BLANK THE WINDOW TO BACK COLOR (This was important at 
			// one point, but not really anymore.  I was going to have an
			// option to change background color, but now I just use a
			// background image, instead.
			g.setColor(Color.getHSBColor(backColorH, backColorS, backColorB));
			g.fillRect(0,0,width,height);
			
			if(!setup)
			{
				//DRAW BACKGROUND
				gameBoard.getBackground().draw(g);
				
				//DRAW SQUARES
				if(gameBoard.getSquareList().size()>0)
				{
					for(int i=0; i<gameBoard.getSquareList().size();i++)
					{
						Vector v = (Vector)gameBoard.getSquareList().get(i);
						if(v.size()>0)
						{
							for(int j=0; j<v.size();j++)
							{
								Square s = (Square)v.get(j);
								s.draw(g);
							}//next j
						}
					}//next i
				}
				
				//DRAW ALL SPRITES IN THE terrainList Vector
				if(!terrainList.isEmpty())
				{
					for (int i=0;i<terrainList.size();i++)
					{
						FlatImage item = (FlatImage) terrainList.get(i);
						if((item.getVisible())&&(!client))
						{
							item.draw(g);
						}else
						{
							if((item.getVisible())&&(client))
							{
								item.draw(g);
							}
						}
					}//next i
				}
				
				//DRAW GRID
				try
				{
					if(showGridCheckBox.getSelected())
					{
						g.setColor(Color.gray);
						int rows = gameBoard.getHeight();
						int cols = gameBoard.getWidth();
						int hdiv = height/squaresPerScreen;
						int hdtro = hdiv*(rows-1);
						int hdtco = hdiv*(cols-1);
						for(int i=(int)Math.floor((0-offsetX.getValue())/hdiv);((i<cols)&&(i<((int)Math.floor((0-offsetX.getValue())/hdiv)+squaresPerScreen+6)));i++)
						{
							g.drawLine(0+offsetX.getValue(), 0+offsetY.getValue(), 0+offsetX.getValue(), hdtro+hdiv+offsetY.getValue());
							g.drawLine((hdiv*i)+hdiv+offsetX.getValue(),offsetY.getValue(),(hdiv*i)+hdiv+offsetX.getValue(),hdtro+hdiv+offsetY.getValue());
						}
						for(int i=(int)Math.floor((0-offsetY.getValue())/hdiv);((i<rows)&&(i<((int)Math.floor((0-offsetY.getValue())/hdiv)+squaresPerScreen+1)));i++)
						{
							g.drawLine(offsetX.getValue(), offsetY.getValue(), hdtco+hdiv+offsetX.getValue(), offsetY.getValue());
							g.drawLine(offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue(),hdtco+hdiv+offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue());
						}
					}//if(showGridCheckBox.getSelected())
				}catch(Exception e)
				{
					g.setColor(Color.gray);
					int rows = gameBoard.getHeight();
					int cols = gameBoard.getWidth();
					int hdiv = height/squaresPerScreen;
					int hdtro = hdiv*(rows-1);
					int hdtco = hdiv*(cols-1);
					for(int i=(int)Math.floor((0-offsetX.getValue())/hdiv);((i<cols)&&(i<((int)Math.floor((0-offsetX.getValue())/hdiv)+squaresPerScreen+6)));i++)
					{
						g.drawLine(0+offsetX.getValue(), 0+offsetY.getValue(), 0+offsetX.getValue(), hdtro+hdiv+offsetY.getValue());
						g.drawLine((hdiv*i)+hdiv+offsetX.getValue(),offsetY.getValue(),(hdiv*i)+hdiv+offsetX.getValue(),hdtro+hdiv+offsetY.getValue());
					}
					for(int i=(int)Math.floor((0-offsetY.getValue())/hdiv);((i<rows)&&(i<((int)Math.floor((0-offsetY.getValue())/hdiv)+squaresPerScreen+1)));i++)
					{
						g.drawLine(offsetX.getValue(), offsetY.getValue(), hdtco+hdiv+offsetX.getValue(), offsetY.getValue());
						g.drawLine(offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue(),hdtco+hdiv+offsetX.getValue(),(hdiv*i)+hdiv+offsetY.getValue());
					}
				}//catch e
				
				//DRAW ALL SPRITES IN THE unitList Vector
				if(!unitList.isEmpty())
				{
					for (int i=0;i<unitList.size();i++)
					{
						FlatImage item = (FlatImage) unitList.get(i);
						if((item.getVisible())&&(!client))
						{
							item.draw(g);
						}else
						{
							try
							{
								Unit u = (Unit)item;
								if((item.getVisible())&&(client)&&(determineCover(myUnit,u)<16)&&((myUnit.getPerception()>=u.getStealth())||((Math.abs(myUnit.getColumn()-u.getColumn())<2)&&(Math.abs(myUnit.getRow()-u.getRow())<2))||(determineCover(myUnit,u)==0)))
								{
									item.draw(g);
								}
							}catch(Exception e)
							{
								if(client)
								{
									item.draw(g);
								}
							}//catch
						}
					}//next i
				}
				//DRAW ALL SPRITES IN THE markerList Vector
				if(!markerList.isEmpty())
				{
					for (int i=0;i<markerList.size();i++)
					{
						FlatImage item = (FlatImage) markerList.get(i);
						if((item.getVisible())&&(!client))
						{
							item.draw(g);
						}else
						{
							if((item.getVisible())&&(client))
							{
								item.draw(g);
							}
						}
					}//next i
				}
				//HUD AREA
				g.setColor(Color.lightGray);
				g.fillRect((int)(width*0.75),0,(int)(width/4),height);
				g.setColor(Color.black);
				g.drawLine((int)(width*0.75), 0, (int)(width*0.75), height);
				for(int i=0;i<buttonList.size();i++)
				{
					Button b = (Button)buttonList.get(i);
					b.draw(g);
				}//next i
				for(int i=0;i<textBoxList.size();i++)
				{
					TextBox t = (TextBox)textBoxList.get(i);
					t.draw(g);
				}//next i
				for(int i=0;i<labelList.size();i++)
				{
					Label l = (Label)labelList.get(i);
					if((l.getName().equals("lblTurnLabel2"))&&(turnUnit!=null))
					{
						l.setText(turnUnit.getName());
					}else if((l.getName().equals("lblTurnLabel2"))&&(turnUnit==null))//if(l.getName().equals("lblTurnLabel2"))
					{
						l.setText("No unit");
					}//else if(turnUnit==null) -- if(l.getName().equals("lblTurnLabel2"))
					l.draw(g);
				}//next i
				try
				{
					showGridCheckBox.draw(g);
				}catch(Exception e)
				{
					
				}//catch e
				
				//DRAW SCROLLBARS
				for(int i=0;i<scrollBarList.size();i++)
				{
					ScrollBar s = (ScrollBar)scrollBarList.get(i);
					s.draw(g);
				}//next i
				
				//Forms
				Vector formRemoval = new Vector();
				for(int i=0;i<formList.size();i++)
				{
					Form f = (Form)formList.get(i);
					f.draw();
					if(!f.isShowing())
					{
						formRemoval.add(f);
					}
				}//next i
				//TODO: add removal lists like the one below for all vectors of objects
				formList.removeAll(formRemoval);
				formRemoval.clear();
				
			}
			
			//BRANCH OUT TO THE THINGS THAT NEED TO BE CALCULATED DURING A LOOP WHEN THE GAME IS "PAUSED"
			doPausedStuff(g);
			
			//DISPOSE OF GRAPHICS OBJECT AND FLIP BUFFER
			g.dispose();
			try
			{
				strategy.show();
			}catch(Exception e)
			{
				createBufferStrategy(2);
				strategy = getBufferStrategy();
				strategy.show();
			}
			
			//I USE THIS TO BRANCH OUT TO EVERYTHING THAT NEEDS TO BE CALCULATED DURING A LOOP
			if((!pause)&(!gameOver)&(!setup))
			{
				doStuff(g);
			}
			
			// remove any unit that has been marked for clear up
			//unitList.removeAll(removeList);
			//removeList.clear();
			
			// PAUSE execution for a few milliseconds
			try
			{
				Thread.sleep(10);
			}
			catch (Exception e)
			{
				
			}
		}
	}//gameloop
	
	public void doPausedStuff(Graphics2D g)
	{
		if(pause)
		{
			// THIS SECTION IS NOT BEING USED IN THE D&D PROGRAM, I JUST PUT IT
			// IN BECAUSE IM RE-USING A CLASS THAT I USE IN OTHER JAVA GAMES.  I
			// LEFT THIS ONE INTACT IN CASE I WANT TO USE IT...
			g.setColor(Color.white);
			g.drawString("paused",(800-g.getFontMetrics().stringWidth("paused"))/2,250);
			g.drawString("Press P to unpause...",(800-g.getFontMetrics().stringWidth("Press P to unpause..."))/2,300);
		}else if(gameOver)
		{
			// THIS IS FOR WHEN THE PLAYER GETS A GAMEOVER...
			// I KNOW THIS DOESN'T MAKE SENSE FOR THIS PROGRAM,
			// BUT I'M RE-USING A CLASS FROM ALL THE GAMES I
			// WRITE IN JAVA, AND I USUSALLY USE THIS SPOT...
		}else if(setup)
		{
			// THIS SECTION IS JUST FOR THE SETUP SCREEN.  THIS IS NOT A GOOD WAY TO DO IT, BUT
			// I WAS LAZY ON MY DESIGN THE DAY I SET THIS UP
			checkKeys();
			g.setColor(Color.lightGray);
			g.fillRect(0,0,width,height);
			g.setColor(Color.black);
			g.drawLine(0, height-90, width, height-90);
			g.drawLine((int)Math.floor(width/2), 0, (int)Math.floor(width/2), height-90);
			g.drawString("Start server",((int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-10-20-20-5-g.getFontMetrics().stringWidth("Start server"))),height-42);
			g.drawString("Start client",(int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-20-5),height-42);
			g.drawString("Server information",10,20);
			g.drawString("Client information",(int)Math.floor(width/2)+10,20);
			g.drawString("Enter server's IP address:",(int)Math.floor(width/2)+20,50);
			g.drawString("Enter your user name:",(int)Math.floor(width/2)+20,80);
			g.drawString("Enter up to 10 user names:",20,50);
			
			if(formList.size()>0)
			{
				for(int i=0; i<formList.size(); i++)
				{
					Form f = (Form)formList.get(i);
					f.draw();
					f.function(g);
				}//next i
			}//if(formList.size()>0)
			
			for(int i=0;i<buttonList.size();i++)
			{
				Button b = (Button)buttonList.get(i);
				if((mouseLeft)&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
				{
					b.setPressed(true);
				}else if((b.getPressed())&&(!mouseLeft)&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
				{
					b.setPressed(false);
					b.action(g);
				}else if(b.getPressed())
				{
					b.setPressed(false);
				}
				b.draw(g);
			}//next i
			for(int i=0;i<textBoxList.size();i++)
			{
				TextBox tb = (TextBox)textBoxList.get(i);
				if(mouseLeft)
				{
					if((mouseX>tb.getX())&&(mouseX<(tb.getX()+tb.getWidth()))&&(mouseY>tb.getY())&&(mouseY<(tb.getY()+tb.getHeight())))
					{
						for(int j=0;j<textBoxList.size();j++)
						{
							TextBox tb2 = (TextBox)textBoxList.get(j);
							if(tb2.getSelected())
							{
								tb2.setSelected(false);
							}
						}//next j
						if(tb.getVisible())
						{
							selectedTextBox=tb;
							tb.setSelected(true);
							mouseLeft=false;
						}
					}
				}
				tb.draw(g);
			}//next i
		}
	}//doPausedStuff
	
	public void doStuff(Graphics2D g)
	{
		checkScrollBars();
		checkMouse(g);
		checkKeys();
		checkForms(g);
		checkButtons(g);
		checkTextBoxes(g);
		moveAI();
		collisionDetection();
		comm();
		if((turnUnit==null)&&(turnList.size()>0))
		{
			if(server)
			{
				turnUnit=(Unit)turnList.get(0);
				turnUnit.setTurn(1);
			}
		}
	}//doStuff
	
	public void checkKeys()
	{
		// I WOULD LIKE TO MOVE THIS SUBROUTINE TO A DIFFERENT
		// CLASS FOR ORGANIZATION PURPOSES, BUT I HAVEN'T DONE
		// IT YET...
		if ((kLeft)&&(!setup))
		{
			//player.moveLeft();
			if((kShift)&&(offsetX.getValue()<5))
			{
				offsetX.setValue(offsetX.getValue()+5);
			}else if(offsetX.getValue()<10)
			{
				offsetX.setValue(offsetX.getValue()+1);
			}
		}
		if ((kRight)&&(!setup))
		{
			//player.moveRight();
			//if(offsetX>-10)
			//{
			if(kShift)
			{
				offsetX.setValue(offsetX.getValue()-5);
			}else
			{
				offsetX.setValue(offsetX.getValue()-1);
			}
			//}
		}
		if ((kUp)&&(!setup))
		{
			//player.moveUp();
			if((kShift)&&(offsetY.getValue()<5))
			{
				offsetY.setValue(offsetY.getValue()+5);
			}else if(kControl)
			{
				squaresPerScreen=squaresPerScreen-1;
				kUp=false;
			}else if(offsetY.getValue()<10)
			{
				offsetY.setValue(offsetY.getValue()+1);
			}
		}
		if ((kDown)&&(!setup))
		{
			//player.moveDown();
			if(kShift)
			{
				offsetY.setValue(offsetY.getValue()-5);
			}else if(kControl)
			{
				squaresPerScreen=squaresPerScreen+1;
				kDown=false;
			}else
			{
				offsetY.setValue(offsetY.getValue()-1);
			}
		}
		if(kSpace)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + " ");
			}
			kSpace = false;
		}
		if (kP)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "P");
					kP=false;
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "p");
					kP=false;
				}
			}
			kP=false;
		}
		if (kQ)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Q");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "q");
				}
				kQ=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveUpLeft();
						u.setMoved(true);
						kQ=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kW)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "W");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "w");
				}
				kW=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveUp();
						u.setMoved(true);
						kW=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kE)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "E");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "e");
				}
				kE=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveUpRight();
						u.setMoved(true);
						kE=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kA)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "A");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "a");
				}
				kA=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveLeft();
						u.setMoved(true);
						kA=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kS)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "S");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "s");
				}
				kS=false;
			}
		}
		if (kD)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "D");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "d");
				}
				kD=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveRight();
						u.setMoved(true);
						kD=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kZ)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Z");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "z");
				}
				kZ=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveDownLeft();
						u.setMoved(true);
						kZ=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kX)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "X");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "x");
				}
				kX=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveDown();
						u.setMoved(true);
						kX=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if (kC)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "C");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "c");
				}
				kC=false;
			}else if(client)
			{
				if(myUnit.getTurn()==1)
				{
					try
					{
						Unit u = (Unit)unitList.get(unitList.indexOf(myUnit));
						u.moveDownRight();
						u.setMoved(true);
						kC=false;
					}catch(Exception e)
					{
						
					}//catch
				}
			}
		}
		if(kB)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "B");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "b");
				}
				kB=false;
			}
		}
		if(kF)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "F");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "f");
				}
				kF=false;
			}
		}
		if(kG)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "G");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "g");
				}
				kG=false;
			}
		}
		if(kH)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "H");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "h");
				}
				kH=false;
			}
		}
		if(kI)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "I");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "i");
				}
				kI=false;
			}
		}
		if(kJ)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "J");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "j");
				}
				kJ=false;
			}
		}
		if(kK)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "K");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "k");
				}
				kK=false;
			}
		}
		if(kL)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "L");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "l");
				}
				kL=false;
			}
		}
		if(kM)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "M");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "m");
				}
				kM=false;
			}
		}
		if(kN)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "N");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "n");
				}
				kN=false;
			}
		}
		if(kO)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "O");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "o");
				}
				kO=false;
			}
		}
		if(kR)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "R");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "r");
				}
				kR=false;
			}
		}
		if(kT)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "T");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "t");
				}
				kT=false;
			}
		}
		if(kU)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "U");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "u");
				}
				kU=false;
			}
		}
		if(kV)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "V");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "v");
				}
				kV=false;
			}
		}
		if(kY)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "Y");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "y");
				}
				kY=false;
			}
		}
		if (k0==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + ")");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "0");
				}
				k0=false;
			}
		}
		if (k1==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "1");
				k1=false;
			}
		}
		if (k2==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "2");
				k2=false;
			}
		}
		if (k3==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "3");
				k3=false;
			}
		}
		if (k4==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "4");
				k4=false;
			}
		}
		if (k5==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "5");
				k5=false;
			}
		}
		if (k6==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "6");
				k6=false;
			}
		}
		if (k7==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "7");
				k7=false;
			}
		}
		if (k8==true)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + "8");
				k8=false;
			}
		}
		if (k9==true)
		{
			if(selectedTextBox!=null)
			{
				if(kShift)
				{
					selectedTextBox.setText(selectedTextBox.getText() + "(");
				}else
				{
					selectedTextBox.setText(selectedTextBox.getText() + "9");
				}
				k9=false;
			}
		}
		if (kPeriod)
		{
			if(selectedTextBox!=null)
			{
				selectedTextBox.setText(selectedTextBox.getText() + ".");
				kPeriod=false;
			}
		}
		if (kBackspace)
		{
			if(selectedTextBox!=null)
			{
				if((selectedTextBox.getText().length()-1)>=0)
				{
					selectedTextBox.setText(selectedTextBox.getText().substring(0,selectedTextBox.getText().length()-1));
					kBackspace=false;
				}
			}
		}
		if (kDelete)
		{
			if(selectedPiece!=null)
			{
				try
				{
					Unit u = (Unit)selectedPiece;
					removeUnit(u);
				}catch(Exception e)
				{
					try
					{
						Terrain t = (Terrain)selectedPiece;
						removeTerrain(t);
					}catch(Exception e2)
					{
						try
						{
							Marker m = (Marker)selectedPiece;
							removeMarker(m);
						}catch(Exception e3)
						{
							try
							{
								Effect ef = (Effect)selectedPiece;
								removeEffect(ef);
							}catch(Exception e4)
							{
								try
								{
									Object o = (Object)selectedPiece;
									removeObject(o);
								}catch(Exception e5)
								{
									
								}//catch e
							}//catch e4
						}//catch e3
					}//catch e2
				}//catch e
				kDelete=false;
			}//if(selectedPiece!=null)
		}//if (kDelete)
	}//checkKeys
	
	public void checkMouse(Graphics2D g)
	{
		if((System.currentTimeMillis()-mouseUpdateTime)>100)
		{
			mouseX = MouseInfo.getPointerInfo().getLocation().x-getLocationOnScreen().x;
			mouseY = MouseInfo.getPointerInfo().getLocation().y-getLocationOnScreen().y;
			if(selectedPiece != null)
			{
				if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))+5)))&&(!movingPiece)&&(!sizingPiece)&&(!client))
				{
					Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
					setCursor(resizeCursor);
				}else if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)+5)))&&(!movingPiece)&&(!sizingPiece))
				{
					if(client)
					{
						if(myUnit.getTurn()==1)
						{
							Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
							setCursor(moveCursor);
						}
					}else
					{
						Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
						setCursor(moveCursor);
					}
				}else
				{
					Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
					setCursor(normalCursor);
				}
			}else
			{
				Cursor normalCursor = new Cursor(Cursor.DEFAULT_CURSOR);
				setCursor(normalCursor);
			}
			mouseUpdateTime=System.currentTimeMillis();
		}//if((System.currentTimeMillis()-mouseUpdateTime)>100)
		if(!(scrolling))
		{
			if(toolBox != null)
			{
				if((mouseLeft)&&(!toolBox.getDragging()))
				{
					mouseLeftPressedChecks(g);
				}//if((mouseLeft)&&(!toolBox.getDragging()))
			}else//if(toolBox != null)
			{
				if(mouseLeft)
				{
					mouseLeftPressedChecks(g);
				}//if(mouseLeft)
			}//else -- if(toolBox != null)
		}//if(!(scrolling))
	}//checkMouse
	
	private void mouseLeftPressedChecks(Graphics2D g)
	{
		if(selectedPiece != null)
		{
			try
			{
				Unit s = (Unit)selectedPiece;
				if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)+5)))&&(!movingPiece)&&(!sizingPiece)&&((!client)||(s.getOwner().equalsIgnoreCase(myUnit.getOwner()))))
				{
					if(client)
					{
						if(myUnit.getTurn()==1)
						{
							movingPiece=true;
						}
					}else
					{
						movingPiece=true;
					}
				}else if(movingPiece)
				{
					dragPiecePosition();
				}else if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))+5)))&&(!sizingPiece)&&(!client))
				{
					sizingPiece=true;
				}else if(sizingPiece)
				{
					dragPieceSize();
				}
				if((doubleClick)&&(!setup))
				{
					determineSelection();
					resolveDoubleClick(g);
					doubleClick=false;
					mouseLeft=false;
				}else if((!movingPiece)&&(!sizingPiece))
				{
					determineSelection();
				}
			}catch(Exception e)
			{
				if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+((selectedPiece.getSizeX()*(height/squaresPerScreen))/2)+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+((selectedPiece.getSizeY()*(height/squaresPerScreen))/2)+5)))&&(!movingPiece)&&(!sizingPiece)&&(!client))
				{
					movingPiece=true;
				}else if(movingPiece)
				{
					dragPiecePosition();
				}else if(((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))-5))&&(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))+5)))&&((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))-5))&&(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))+5)))&&(!sizingPiece)&&(!client))
				{
					sizingPiece=true;
				}else if(sizingPiece)
				{
					dragPieceSize();
				}
				if((doubleClick)&&(!setup))
				{
					determineSelection();
					resolveDoubleClick(g);
					doubleClick=false;
					mouseLeft=false;
				}else if((!movingPiece)&&(!sizingPiece))
				{
					determineSelection();
				}
			}
		}else//if(selectedPiece != null)
		{
			if((doubleClick)&&(!setup))
			{
				determineSelection();
				resolveDoubleClick(g);
				doubleClick=false;
				mouseLeft=false;
			}else if((!movingPiece)&&(!sizingPiece))
			{
				determineSelection();
			}
		}//else -- if(selectedPiece != null)
	}//private void mouseLeftPressedChecks(Graphics2D g)
	
	private void dragPiecePosition()
	{
		Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
		setCursor(moveCursor);
		mouseX = MouseInfo.getPointerInfo().getLocation().x-getLocationOnScreen().x;
		mouseY = MouseInfo.getPointerInfo().getLocation().y-getLocationOnScreen().y;
		if((mouseX-offsetX.getValue())>(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))+(height/(2*squaresPerScreen))))
		{
			try
			{
				Unit p = (Unit)selectedPiece;
				p.moveRight();
				p.setMoved(true);
			}catch(Exception e)
			{
				try
				{
					Terrain p = (Terrain)selectedPiece;
					p.moveRight();
				}catch(Exception e2)
				{
					try
					{
						Marker p = (Marker)selectedPiece;
						p.moveRight();
					}catch(Exception e3)
					{
						try
						{
							Effect p = (Effect)selectedPiece;
							p.moveRight();
						}catch(Exception e4)
						{
							Object p = (Object)selectedPiece;
							p.moveRight();
						}//catch
					}//catch
				}//catch
			}//catch
		}//if(mouseX>(((selectedPiece.getX()*20)/squaresPerScreen)+(selectedPiece.getSizeX()*(height/squaresPerScreen))+(height/(2*squaresPerScreen))))
		if(((mouseX-offsetX.getValue())<(((selectedPiece.getX()*20)/squaresPerScreen)-(height/(2*squaresPerScreen)))))
		{
			try
			{
				Unit p = (Unit)selectedPiece;
				p.moveLeft();
				p.setMoved(true);
			}catch(Exception e)
			{
				try
				{
					Terrain p = (Terrain)selectedPiece;
					p.moveLeft();
				}catch(Exception e2)
				{
					try
					{
						Marker p = (Marker)selectedPiece;
						p.moveLeft();
					}catch(Exception e3)
					{
						try
						{
							Effect p = (Effect)selectedPiece;
							p.moveLeft();
						}catch(Exception e4)
						{
							Object p = (Object)selectedPiece;
							p.moveLeft();
						}//catch
					}//catch
				}//catch
			}//catch
		}//if((mouseX<(((selectedPiece.getX()*20)/squaresPerScreen)-(height/(2*squaresPerScreen)))))
		if((mouseY-offsetY.getValue())>(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))+(height/(2*squaresPerScreen))))
		{
			try
			{
				Unit p = (Unit)selectedPiece;
				p.moveDown();
				p.setMoved(true);
			}catch(Exception e)
			{
				try
				{
					Terrain p = (Terrain)selectedPiece;
					p.moveDown();
				}catch(Exception e2)
				{
					try
					{
						Marker p = (Marker)selectedPiece;
						p.moveDown();
					}catch(Exception e3)
					{
						try
						{
							Effect p = (Effect)selectedPiece;
							p.moveDown();
						}catch(Exception e4)
						{
							Object p = (Object)selectedPiece;
							p.moveDown();
						}//catch
					}//catch
				}//catch
			}//catch
		}//if(mouseY>(((selectedPiece.getY()*20)/squaresPerScreen)+(selectedPiece.getSizeY()*(height/squaresPerScreen))+(height/(2*squaresPerScreen))))
		if(((mouseY-offsetY.getValue())<(((selectedPiece.getY()*20)/squaresPerScreen)-(height/(2*squaresPerScreen)))))
		{
			try
			{
				Unit p = (Unit)selectedPiece;
				p.moveUp();
				p.setMoved(true);
			}catch(Exception e)
			{
				try
				{
					Terrain p = (Terrain)selectedPiece;
					p.moveUp();
				}catch(Exception e2)
				{
					try
					{
						Marker p = (Marker)selectedPiece;
						p.moveUp();
					}catch(Exception e3)
					{
						try
						{
							Effect p = (Effect)selectedPiece;
							p.moveUp();
						}catch(Exception e4)
						{
							Object p = (Object)selectedPiece;
							p.moveUp();
						}//catch
					}//catch
				}//catch
			}//catch
		}//if((mouseY<(((selectedPiece.getY()*20)/squaresPerScreen)-(height/(2*squaresPerScreen)))))
	}//dragPiece()
	
	public void dragPieceSize()
	{
		Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
		setCursor(resizeCursor);
		mouseX = MouseInfo.getPointerInfo().getLocation().x-getLocationOnScreen().x;
		mouseY = MouseInfo.getPointerInfo().getLocation().y-getLocationOnScreen().y;
		int r=0;
		int c=0;
		int pc=0;
		int pr=0;
		c=(int)Math.floor((mouseX-offsetX.getValue()-((height/squaresPerScreen)/2))/(height/squaresPerScreen));
		r=(int)Math.floor((mouseY-offsetY.getValue()-((height/squaresPerScreen)/2))/(height/squaresPerScreen));
		pc=(int)Math.floor((selectedPiece.getX())/(height/squaresPerScreen));
		pr=(int)Math.floor((selectedPiece.getY())/(height/squaresPerScreen));
		if(c>(pc+selectedPiece.getSizeX()-1))
		{
			selectedPiece.setSizeX(selectedPiece.getSizeX()+1);
			try
			{
				Unit s = (Unit)selectedPiece;
				s.setMoved(true);
			}catch(Exception e)
			{
				
			}
		}else if((c<(pc+selectedPiece.getSizeX()-1))&&(selectedPiece.getSizeX()>1))
		{
			selectedPiece.setSizeX(selectedPiece.getSizeX()-1);
			try
			{
				Unit s = (Unit)selectedPiece;
				s.setMoved(true);
			}catch(Exception e)
			{
				
			}
		}
		if(r>(pr+selectedPiece.getSizeY()-1))
		{
			selectedPiece.setSizeY(selectedPiece.getSizeY()+1);
			try
			{
				Unit s = (Unit)selectedPiece;
				s.setMoved(true);
			}catch(Exception e)
			{
				
			}
		}else if((r<(pr+selectedPiece.getSizeY()-1))&&(selectedPiece.getSizeY()>1))
		{
			selectedPiece.setSizeY(selectedPiece.getSizeY()-1);
			try
			{
				Unit s = (Unit)selectedPiece;
				s.setMoved(true);
			}catch(Exception e)
			{
				
			}
		}
		try
		{
			Terrain terr = (Terrain)selectedPiece;
			for(int i =0; i<terr.getSizeX(); i++)
			{
				for(int j =0; j<terr.getSizeY(); j++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(terr.getRow()+j);
					Square sq = (Square)v.get(terr.getColumn()+i);
					sq.getOccupees().remove(this);
				}//next j
			}//next i
			for(int i =0; i<terr.getSizeX(); i++)
			{
				for(int j =0; j<terr.getSizeY(); j++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(terr.getRow()+j);
					Square sq = (Square)v.get(terr.getColumn()+i);
					sq.getOccupees().add(this);
				}//next j
			}//next i
		}catch(Exception e)
		{
			
		}
	}//dragPieceSize()
	
	public int determineCover(Unit piece, FlatImage target)
	{
		if(true)
		{
			return 0;//For now - I want this subroutine out until I can fix it.
		}
		//TODO: Make this work for full cover.  Make this work for units that are larger than 1 square in size
		// 0 = no cover; 1-15 = partial cover; 16 = full cover
		int cover=0;
		try
		{
			Unit tUnit = (Unit)target;
			if(piece.getRow()==tUnit.getRow())
			{
				if(piece.getColumn()!=tUnit.getColumn())
				{
					int c = piece.getColumn()+(1*((tUnit.getColumn()-piece.getColumn())/((int)Math.abs(tUnit.getColumn()-piece.getColumn()))));
					while(c!=tUnit.getColumn())
					{
						Vector v = (Vector)gameBoard.getSquareList().get(piece.getRow());
						Square s = (Square)v.get(c);
						if(s.getOccupees().size()>0)
						{
							for(int i=0;i<s.getOccupees().size();i++)
							{
								try
								{
									Terrain t =(Terrain)s.getOccupees().get(i);
									if(t.getObstruction())
									{
										return 16;
									}//if(t.getObstruction())
								}catch(Exception e)
								{
									
								}//catch e
							}//next i
						}//if(s.getOccupees().size()>0)
						c=c+(1*((tUnit.getColumn()-piece.getColumn())/((int)Math.abs(tUnit.getColumn()-piece.getColumn()))));
					}//loop
				}else//if(c!=tUnit.getColumn())
				{
					return 0;
				}//else -- else if(piece.getColumn()>tUnit.getColumn())
			}else if(piece.getColumn()==tUnit.getColumn())
			{
				int r = piece.getRow()+(1*((tUnit.getRow()-piece.getRow())/((int)Math.abs(tUnit.getRow()-piece.getRow()))));
				while(r!=tUnit.getRow())
				{
					Vector v = (Vector)gameBoard.getSquareList().get(r);
					Square s = (Square)v.get(piece.getColumn());
					if(s.getOccupees().size()>0)
					{
						for(int i=0;i<s.getOccupees().size();i++)
						{
							try
							{
								Terrain t =(Terrain)s.getOccupees().get(i);
								if(t.getObstruction())
								{
									return 16;
								}//if(t.getObstruction())
							}catch(Exception e)
							{
								
							}//catch e
						}//next i
					}//if(s.getOccupees().size()>0)
					r=r+(1*((tUnit.getRow()-piece.getRow())/((int)Math.abs(tUnit.getRow()-piece.getRow()))));
				}//loop
			}else//else if(piece.getColumn()==tUnit.getColumn())
			{
				int r1 = piece.getRow();
				int c1 = piece.getColumn();
				for(int i=0; i<4; i++)
				{
					int posX = 0;
					int posY = 0;
					int posXt = 0;
					int posYt = 0;
					int blockedTargetCorners=0;
					//Check which corner:
					if(i==0)
					{
						posXt = piece.getX();
						posYt = piece.getY();
					}else if(i==1)
					{
						posXt = piece.getX()+(30*piece.getSizeX());
						posYt = piece.getY();
					}else if(i==2)
					{
						posXt = piece.getX();
						posYt = piece.getY()+(30*piece.getSizeY());
					}else
					{
						posXt = piece.getX()+(30*piece.getSizeX());
						posYt = piece.getY()+(30*piece.getSizeY());
					}
					for(int j=0; j<4; j++)
					{
						int destX=0;
						int destY=0;
						posX=posXt;
						posY=posYt;
						//Check which corner:
						if(i==0)
						{
							destX = target.getX();
							destY = target.getY();
						}else if(i==1)
						{
							destX = target.getX()+(30*target.getSizeX());
							destY = target.getY();
						}else if(i==2)
						{
							destX = target.getX();
							destY = target.getY()+(30*target.getSizeY());
						}else
						{
							destX = target.getX()+(30*target.getSizeX());
							destY = target.getY()+(30*target.getSizeY());
						}
						int distance=(int)Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)));
						double angle=Math.atan((destY-posY)/(destX-posX));
						while(Math.abs(Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)))) > 2)
						{
							distance=(int)Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)));
							angle=Math.atan((destY-posY)/(destX-posX));
							if(destX>posX)
							{
								posX=posX + (int)Math.abs(2*Math.cos(angle));
								if(destY>posY)
								{
									posY=posY + (int)Math.abs(2*Math.sin(angle));
								}else//if(destY>posY)
								{
									posY=posY - (int)Math.abs(2*Math.sin(angle));
								}//else -- if(destY>posY)
							}else//if(destX>posX)
							{
								posX=posX - (int)Math.abs(2*Math.cos(angle));
								if(destY>posY)
								{
									posY=posY + (int)Math.abs(2*Math.sin(angle));
								}else//if(destY>posY)
								{
									posY=posY - (int)Math.abs(2*Math.sin(angle));
								}//else -- if(destY>posY)
							}//else -- if(destX>posX)
							int r=0;
							int c=0;
							c=(int)Math.floor(posX/30);
							r=(int)Math.floor(posY/30);
							if((c!=c1)||(r!=r1))
							{
								Vector v = (Vector)gameBoard.getSquareList().get(r);
								Square s = (Square)v.get(c);
								if(s.getOccupees().size()>0)
								{
									if(s.getOccupees().contains(piece))
									{
										posX=destX;
										posY=destY;
									}else//if(s.getOccupees().contains(piece))
									{
										for(int h=0; h<s.getOccupees().size(); h++)
										{
											try
											{
												Terrain t = (Terrain)s.getOccupees().get(h);
												if(t.getObstruction())
												{
													blockedTargetCorners=blockedTargetCorners+1;
													posX=destX;
													posY=destY;
												}//if(t.getObstruction())
											}catch(Exception e)
											{
												
											}//catch
										}//next h
									}//else -- if(s.getOccupees().contains(piece))
								}//if(s.getOccupees().size()>0)
								c1=c;
								r1=r;
							}//if((c!=c1)||(r!=r1))
						}//loop
					}//next j
					if(blockedTargetCorners>0)
					{
						cover=cover+blockedTargetCorners;
					}
				}//next i
			}//else -- else if(piece.getColumn()==tUnit.getColumn())
		}catch(Exception e)
		{
			int r1 = piece.getRow();
			int c1 = piece.getColumn();
			for(int i=0; i<4; i++)
			{
				int posX = 0;
				int posY = 0;
				int posXt = 0;
				int posYt = 0;
				int blockedTargetCorners=0;
				//Check which corner:
				if(i==0)
				{
					posXt = piece.getX();
					posYt = piece.getY();
				}else if(i==1)
				{
					posXt = piece.getX()+(30*piece.getSizeX());
					posYt = piece.getY();
				}else if(i==2)
				{
					posXt = piece.getX();
					posYt = piece.getY()+(30*piece.getSizeY());
				}else
				{
					posXt = piece.getX()+(30*piece.getSizeX());
					posYt = piece.getY()+(30*piece.getSizeY());
				}
				for(int j=0; j<4; j++)
				{
					int destX=0;
					int destY=0;
					posX=posXt;
					posY=posYt;
					//Check which corner:
					if(i==0)
					{
						destX = target.getX();
						destY = target.getY();
					}else if(i==1)
					{
						destX = target.getX()+(30*target.getSizeX());
						destY = target.getY();
					}else if(i==2)
					{
						destX = target.getX();
						destY = target.getY()+(30*target.getSizeY());
					}else
					{
						destX = target.getX()+(30*target.getSizeX());
						destY = target.getY()+(30*target.getSizeY());
					}
					int distance=(int)Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)));
					double angle=Math.atan((destY-posY)/(destX-posX));
					while(Math.abs(Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)))) > 2)
					{
						distance=(int)Math.sqrt((destX-posX)*(destX-posX))+(((destY-posY))*((destY-posY)));
						angle=Math.atan((destY-posY)/(destX-posX));
						if(destX>posX)
						{
							posX=posX + (int)Math.abs(2*Math.cos(angle));
							if(destY>posY)
							{
								posY=posY + (int)Math.abs(2*Math.sin(angle));
							}else//if(destY>posY)
							{
								posY=posY - (int)Math.abs(2*Math.sin(angle));
							}//else -- if(destY>posY)
						}else//if(destX>posX)
						{
							posX=posX - (int)Math.abs(2*Math.cos(angle));
							if(destY>posY)
							{
								posY=posY + (int)Math.abs(2*Math.sin(angle));
							}else//if(destY>posY)
							{
								posY=posY - (int)Math.abs(2*Math.sin(angle));
							}//else -- if(destY>posY)
						}//else -- if(destX>posX)
						int r=0;
						int c=0;
						c=(int)Math.floor(posX/30);
						r=(int)Math.floor(posY/30);
						if((c!=c1)||(r!=r1))
						{
							Vector v = (Vector)gameBoard.getSquareList().get(r);
							Square s = (Square)v.get(c);
							if(s.getOccupees().size()>0)
							{
								if(s.getOccupees().contains(piece))
								{
									posX=destX;
									posY=destY;
								}else//if(s.getOccupees().contains(piece))
								{
									for(int h=0; h<s.getOccupees().size(); h++)
									{
										try
										{
											Terrain t = (Terrain)s.getOccupees().get(h);
											if(t.getObstruction())
											{
												blockedTargetCorners=blockedTargetCorners+1;
												posX=destX;
												posY=destY;
											}//if(t.getObstruction())
										}catch(Exception e2)
										{
											
										}//catch
									}//next h
								}//else -- if(s.getOccupees().contains(piece))
							}//if(s.getOccupees().size()>0)
							c1=c;
							r1=r;
						}//if((c!=c1)||(r!=r1))
					}//loop
				}//next j
				if(blockedTargetCorners>0)
				{
					cover=cover+blockedTargetCorners;
				}
			}//next i
		}//catch e
		return cover;
	}//private boolean fullCover(FlatImage piece)
	
	public void comm()
	{
		if(server)
		{
			// UMM... THIS WAS JUST HERE FOR TESTING.  OBVIOUSLY THIS WOULD NOT
			// WORK FOR A MULTI-THREADED SERVER.  THIS IS OUT SINCE I SET UP
			// THE MULTI-THREADED SERVER.
			
			//listener.listen();
			/*MultiThreadedServer mTServer = new MultiThreadedServer(this,9000);
			new Thread(mTServer).start();*/

			/*try {
			    Thread.sleep(20);
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}*/
			//System.out.println("Stopping Server");
			//mTServer.stop();

		}else if(client)
		{
			// YEAH... I HAVEN'T SET UP A MULTI-THREADED CLIENT, YET...BECAUSE I DON'T FEEL LIKE
			// DOING IT.  I SHOULD, THOUGH.
			// TODO: Multi-threaded client (eventually)
			if(((System.currentTimeMillis()-lastCommTime)>500)||((System.currentTimeMillis()-lastCommTime)<0))
			{
				talker.call();
				lastCommTime = System.currentTimeMillis();
			}
		}
	}//comm
	
	public void clearSelectedSquare()
	{
		selectedSquare=null;
		if(gameBoard.getSquareList().size()>0)
		{
			for(int i=0; i<gameBoard.getSquareList().size();i++)
			{
				Vector v = (Vector)gameBoard.getSquareList().get(i);
				if(v.size()>0)
				{
					for(int j=0; j<v.size();j++)
					{
						Square s = (Square)v.get(j);
						s.setSelected(false);
					}//next j
				}
			}//next i
		}
	}//clearSelectedSquare()
	
	public void determineSelection()
	{
		selectedPiece=null;
		selectedSquare=null;
		if(gameBoard.getSquareList().size()>0)
		{
			for(int i=0; i<gameBoard.getSquareList().size();i++)
			{
				Vector v = (Vector)gameBoard.getSquareList().get(i);
				if(v.size()>0)
				{
					for(int j=0; j<v.size();j++)
					{
						Square s = (Square)v.get(j);
						s.setSelected(false);
					}//next j
				}
			}//next i
		}
		determineSelectedPiece();
		if(selectedPiece==null)
		{
			determineSelectedSquare();
		}else
		{
			mouseLeft=false;
		}
		if((selectedPiece==null)&&(selectedSquare==null))
		{
			CheckBox cK = showGridCheckBox;
			if((mouseX>cK.getX())&&(mouseX<cK.getX()+12)&&(mouseY>cK.getY())&&(mouseY<cK.getY()+12))
			{
				if(cK.getSelected())
				{
					cK.setSelected(false);
					mouseLeft=false;
				}else
				{
					cK.setSelected(true);
					mouseLeft=false;
				}
			}//if((mouseX>cK.getX())&&(mouseX<cK.getX()+12)&&(mouseY>cK.getY())&&(mouseY<cK.getY()+12))
		}else
		{
			mouseLeft=false;
		}
	}//determineSelection()
	
	public void determineSelectedPiece()
	{
		if(mouseX>(width*0.75))
		{
			//This area is for the user's HUD
			
		}else
		{
			selectedPiece=null;
			int r=0;
			int c=0;
			c=(int)Math.floor((mouseX-offsetX.getValue())/(height/squaresPerScreen));
			r=(int)Math.floor((mouseY-offsetY.getValue())/(height/squaresPerScreen));
			if(!effectList.isEmpty())
			{
				for(int i=(effectList.size()-1);i>=0;i--)
				{
					Effect p = (Effect)effectList.get(i);
					if((r >= p.getRow())&&(r <= (p.getRow()+p.getSizeY()-1))&&(c >= p.getColumn())&&(c <= (p.getColumn()+p.getSizeX()-1))&&(!client))
					{
						selectedPiece = p;
						i=-1;
					}
				}//next i
			}
			if((!markerList.isEmpty())&&(selectedPiece==null)&&(!doubleClick)&&(!client))
			{
				for(int i=(markerList.size()-1);i>=0;i--)
				{
					Marker p = (Marker)markerList.get(i);
					if((r >= p.getRow())&&(r <= (p.getRow()+p.getSizeY()-1))&&(c >= p.getColumn())&&(c <= (p.getColumn()+p.getSizeX()-1))&&(!client))
					{
						selectedPiece = p;
						i=-1;
					}
				}//next i
			}
			if((!unitList.isEmpty())&&(selectedPiece==null))
			{
				for(int i=(unitList.size()-1);i>=0;i--)
				{
					Unit p = (Unit)unitList.get(i);
					if((r >= p.getRow())&&(r <= (p.getRow()+p.getSizeY()-1))&&(c >= p.getColumn())&&(c <= (p.getColumn()+p.getSizeX()-1))&&((!client)||(p == myUnit)))
					{
						selectedPiece = p;
						i=-1;
					}
				}//next i
			}
			if((!objectList.isEmpty())&&(selectedPiece==null))
			{
				for(int i=(objectList.size()-1);i>=0;i--)
				{
					Object p = (Object)objectList.get(i);
					if((r >= p.getRow())&&(r <= (p.getRow()+p.getSizeY()-1))&&(c >= p.getColumn())&&(c <= (p.getColumn()+p.getSizeX()-1))&&(!client))
					{
						selectedPiece = p;
						i=-1;
					}
				}//next i
			}
			if((!terrainList.isEmpty())&&(selectedPiece==null))
			{
				for(int i=(terrainList.size()-1);i>=0;i--)
				{
					Terrain p = (Terrain)terrainList.get(i);
					if((r >= p.getRow())&&(r <= (p.getRow()+p.getSizeY()-1))&&(c >= p.getColumn())&&(c <= (p.getColumn()+p.getSizeX()-1))&&(!client))
					{
						selectedPiece = p;
						i=-1;
					}
				}//next i
			}//if((!terrainList.isEmpty())&&(selectedPiece==null))
		}//else (the if before the else: if(mouseX>(width*0.75)))
	}//determineSelectedPiece()
	
	public void determineSelectedSquare()
	{
		if(mouseX>(width*0.75))
		{
			//This area is for the user's HUD
			
		}else
		{
			//This is the game board
			int r=0;
			int c=0;
			c=(int)Math.floor((mouseX-offsetX.getValue())/(height/squaresPerScreen));
			r=(int)Math.floor((mouseY-offsetY.getValue())/(height/squaresPerScreen));
			if(gameBoard.getSquareList().size()>0)
			{
				for(int i=0; i<gameBoard.getSquareList().size();i++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(i);
					if(v.size()>0)
					{
						for(int j=0; j<v.size();j++)
						{
							Square s = (Square)v.get(j);
							s.setSelected(false);
						}//next j
					}
				}//next i
			}
			try
			{
				Vector v = (Vector)gameBoard.getSquareList().get(r);
				Square s = (Square)v.get(c);
				s.setSelected(true);
				selectedSquare=s;
			}catch(Exception e)
			{
				
			}//catch
		}//else
	}//determineSelectedSquare()
	
	private void resolveDoubleClick(Graphics2D g)
	{
		if(mouseX<(0.75*width))
		{
			if(selectedSquare != null)
			{
				if(selectedSquare.getOccupees().size()!=1)
				{
					formList.add(new SquareForm("Square: (" + selectedSquare.getColumn() + "," + selectedSquare.getRow() + ")",this, 10, 10, 400, 400, "Square: (" + selectedSquare.getColumn() + "," + selectedSquare.getRow() + ")"));
					SquareForm f = (SquareForm)formList.get(formList.size()-1);
					f.getLabels().add(new Label("lblChooseSelection","Choose the item you wish to select:",5,15,f.getWidth(),20,true));
					if(!client)
					{
						f.getButtons().add(new Button(this,10,170,true,0,"Add piece",Color.getHSBColor(57, 52, 84),g,true));
					}//if(!client)
					int h=0;
					int v=0;
					for(int i=0;i<selectedSquare.getOccupees().size();i++)
					{
						FlatImage o = (FlatImage)selectedSquare.getOccupees().get(i);
						f.getImages().add(new FormImage(o.getName(), o.getRef(), this, (30*h)+(10*(h+1)),(30*v)+(10*(v+1))+20, 30, 30));
						h=h+1;
						if(h>=10)
						{
							h=0;
							v=v+1;				
						}
						f.getPieces().add(o);
						o=(FlatImage)f.getImages().get(f.getImages().size()-1);
						o.setOnBoard(false);
					}//next i
				}else//if(selectedSquare.getOccupees().size()!=1)
				{
					FlatImage o = (FlatImage)selectedSquare.getOccupees().get(0);
					formList.add(new StatsForm("frm" + o.getName(),this, 10, 10, 400, 400, "Info: " + o.getName(),o));
				}//else -- if(selectedSquare.getOccupees().size()!=1)
			}else//if(selectedSquare != null)
			{
				FlatImage o = selectedPiece;
				formList.add(new StatsForm("frm" + o.getName(),this, 10, 10, 400, 400, "Info: " + o.getName(),o));
			}//else -- if(selectedSquare != null)
		}//if(mouseX<(0.75*width))
	}//resolveDoubleClick
	
	private void checkForms(Graphics2D g)
	{
		for(int i=0;i<formList.size();i++)
		{
			Form f = (Form)formList.get(i);
			f.function(g);
		}//next i
	}//checkForms
	
	private void checkScrollBars()
	{
		for(int i=0;i<scrollBarList.size();i++)
		{
			ScrollBar s = (ScrollBar)scrollBarList.get(i);
			s.function(this);
		}//next i
	}//checkScrollBars()
	
	private void checkButtons(Graphics2D g)
	{
		for(int i=0;i<buttonList.size();i++)
		{
			Button b = (Button) buttonList.get(i);
			if((mouseLeft)&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
			{
				b.setPressed(true);
			}else if((b.getPressed())&&(!mouseLeft)&&(mouseX>b.getX())&&(mouseX<(b.getX()+b.getWidth()))&&(mouseY>b.getY())&&(mouseY<(b.getY()+b.getHeight())))
			{
				b.setPressed(false);
				b.action(g);
			}else if((b.getPressed())&&(!mouseLeft))
			{
				b.setPressed(false);
			}
		}//next i
	}//checkButtons
	
	private void checkTextBoxes(Graphics2D g)
	{
		for(int i=0;i<textBoxList.size();i++)
		{
			TextBox t = (TextBox) textBoxList.get(i);
			if((mouseLeft)&&(mouseX>t.getX())&&(mouseX<(t.getX()+t.getWidth()))&&(mouseY>t.getY())&&(mouseY<(t.getY()+t.getHeight())))
			{
				if(selectedTextBox!=null)
				{
					selectedTextBox.setSelected(false);
				}
				t.setSelected(true);
				selectedTextBox=t;
				mouseLeft=false;
				i=textBoxList.size();
			}
		}//next i
	}//checkTextBoxes
	
	public void moveAI()
	{
		// THERE IS NO AI IN THIS PROGRAM.  I JUST LEFT THIS ROUTINE
		// IN THE LOOP BECAUSE I USE IT IN MY GAMES TO BRANCH TO ALL
		// THE ROUTINES FOR AI CALCULATIONS.  I PLAN TO RENAME IT AND
		// USE IT FOR ANIMATIONS IF I PUT ANY IN.
	}//moveAI
	
	private void collisionDetection()
	{
		// NOT IN USE YET - PROBABLY WON'T BE.
	}//collisionDetection
	
	private void resolveCollision(FlatImage item1, FlatImage item2)
	{
		// NOT IN USE YET - PROBABLY WON'T BE.
	}//resolveCollision
	
	public void destroyForm(Graphics2D g)
	{
		// THIS ROUTINE IS FOR WHEN THE USER EXITS THE SETUP SCREEN.
		// AGAIN - NOT THE BEST WAY TO DO THAT SCREEN, BUT I'M LAZY.
		// AS A RESULT, THIS IS KIND OF THE SETUP FOR THE MAIN PART
		// OF THE PROGRAM...
		
		if(server)
		{
			gameBoard=new Board(50,50,new Background("Background",backName,this,0,0,50,50),unitList,terrainList);
			int rows = gameBoard.getHeight();
			int cols = gameBoard.getWidth();
			int hdiv = height/squaresPerScreen;
			for(int i=0;i<rows;i++)
			{
				for(int j=0;j<cols;j++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(i);
					v.add(new Square("Square "+j+","+i,this,j*hdiv,i*hdiv,false,i,j));
				}//next j
			}//next i
			MultiThreadedServer mTServer = new MultiThreadedServer(this,9000);
			new Thread(mTServer).start();
			for(int i=0;i<textBoxList.size();i++)
			{
				TextBox tb = (TextBox) textBoxList.get(i);
				if(tb.getText().length()>0)
				{
					addUnit(new Unit("Unit: Owner = "+tb.getText(),this,"sprites/default.png",tb.getText(),0,i,1,1,0));
					try
					{
						clientList.add(new ClientPlayer(tb.getText(),(Unit)unitList.get(unitList.size()-1)));
					}catch(Exception e)
					{
						
					}//catch e
				}
			}//next i
			textBoxList.clear();
			buttonList.clear();
			textBoxList.add(new TextBox("BoardName",(int)Math.floor(width*0.75)+20,450,200,20,true,0));
			labelList.add(new Label("lblTurnLabel","It is the following player's turn: ",(int)Math.floor(width*0.75)+20,20,100,20,true));
			try
			{
				labelList.add(new Label("lblTurnLabel2",turnUnit.getName(),(int)Math.floor(width*0.75)+20,40,100,20,true));
			}catch(Exception e)
			{
				labelList.add(new Label("lblTurnLabel2","ERROR",(int)Math.floor(width*0.75)+20,40,100,20,true));
			}//catch e
			buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,60,true,0,"Launch toolbox",Color.getHSBColor(57, 52, 84),g,true));
			buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,90,true,0,"End turn",Color.getHSBColor(57, 52, 84),g,true));
			buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,480,true,0,"Save board",Color.getHSBColor(57, 52, 84),g,true));
			buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,510,true,0,"Load board",Color.getHSBColor(57, 52, 84),g,true));
			buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,540,true,0,"Change back",Color.getHSBColor(57, 52, 84),g,true));
			showGridCheckBox = new CheckBox("chkShowGrid",(int)Math.floor(width*0.75)+20, 120, true, "Show grid");
			toolBox = new ToolBox("Toolbox", this, 5, 5, 300, 600, "Toolbox");
			formList.add(toolBox);
			scrollBarList.clear();
			scrollBarList.add(new ScrollBar((int)Math.floor(width*0.75)-20,0,20,height,true,offsetY,30*gameBoard.getHeight()));
			scrollBarList.add(new ScrollBar(0,height-20,20,(int)Math.floor(width*0.75)-20,false,offsetX,30*gameBoard.getWidth()));
			selectedTextBox=null;
			setup=false;
		}else//if(server)
		{
			TextBox tb = (TextBox) textBoxList.get(textBoxList.size()-2);
			if(tb.getText().length()>0)
			{
				serverIP=tb.getText();
				tb = (TextBox) textBoxList.get(textBoxList.size()-1);
				if(tb.getText().length()>0)
				{
					userName=tb.getText();
					gameBoard=new Board(50,50,new Background("Background",backName,this,0,0,50,50),unitList,terrainList);
					int rows = gameBoard.getHeight();
					int cols = gameBoard.getWidth();
					int hdiv = height/squaresPerScreen;
					for(int i=0;i<rows;i++)
					{
						for(int j=0;j<cols;j++)
						{
							Vector v = (Vector)gameBoard.getSquareList().get(i);
							v.add(new Square("Square "+j+","+i,this,j*hdiv,i*hdiv,false,i,j));
						}//next j
					}//next i
					talker = new Client(this);
					talker.setReturnMessage("!MAP");
					textBoxList.clear();
					buttonList.clear();
					
					//buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,60,true,0,"Update map",Color.getHSBColor(57, 52, 84),g,true));
					buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,60,true,0,"New map view",Color.getHSBColor(57, 52, 84),g,true));
					buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,90,true,0,"End turn",Color.getHSBColor(57, 52, 84),g,true));
					showGridCheckBox = new CheckBox("chkShowGrid",(int)Math.floor(width*0.75)+20, 120, true, "Show grid");
					labelList.add(new Label("lblTurnLabel","It is the following player's turn: ",(int)Math.floor(width*0.75)+20,20,100,20,true));
					try
					{
						labelList.add(new Label("lblTurnLabel2",turnUnit.getName(),(int)Math.floor(width*0.75)+20,40,100,20,true));
					}catch(Exception e)
					{
						labelList.add(new Label("lblTurnLabel2","ERROR",(int)Math.floor(width*0.75)+20,40,100,20,true));
					}//catch e
					scrollBarList.clear();
					scrollBarList.add(new ScrollBar((int)Math.floor(width*0.75)-20,0,20,height,true,offsetY,30*gameBoard.getHeight()));
					scrollBarList.add(new ScrollBar(0,height-20,20,(int)Math.floor(width*0.75)-20,false,offsetX,30*gameBoard.getWidth()));
					selectedTextBox=null;
					setup=false;
				}else
				{
					formList.add(new Form("frmErrorStartClientNoName",this,240,240,320,180,"ERROR: No user name!"));
					Form f = (Form)formList.get(formList.size()-1);
					f.getButtons().add(new Button(this,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
					f.getLabels().add(new Label("lblERRORLabel","ERROR: You have to specify your user name.",10,90,145,20,true));
				}
			}else//if(tb.getText().length()>0)
			{
				formList.add(new Form("frmErrorStartClientNoIP",this,240,240,320,180,"ERROR: No IP address!"));
				Form f = (Form)formList.get(formList.size()-1);
				f.getButtons().add(new Button(this,280,155,true,50,"Okay",Color.getHSBColor(57, 52, 84),g,true));
				f.getLabels().add(new Label("lblERRORLabel","ERROR: You have to specify the server's IP.",10,90,145,20,true));
			}//else -- if(tb.getText().length()>0)
		}//else -- if(server)
	}//destroyForm()
	
	public String compress(String eString)
	{
		String cMess = eString;
		cMess.replace(",0,","`");
		cMess.replace(",1,","`1");
		cMess.replace(",2,","`2");
		cMess.replace(",3,","`3");
		cMess.replace(",4,","`4");
		cMess.replace(",5,","`5");
		cMess.replace(",6,","`6");
		cMess.replace(",7,","`7");
		cMess.replace(",8,","`8");
		cMess.replace(",9,","`9");
		cMess.replace("sprites/","`/");
		cMess.replace(".png","`p");
		return cMess;
	}//compress(String eString)
	
	public String decompress(String eString)
	{
		String cMess = eString;
		cMess.replace("`1",",1,");
		cMess.replace("`2",",2,");
		cMess.replace("`3",",3,");
		cMess.replace("`4",",4,");
		cMess.replace("`5",",5,");
		cMess.replace("`6",",6,");
		cMess.replace("`7",",7,");
		cMess.replace("`8",",8,");
		cMess.replace("`9",",9,");
		cMess.replace("`",",0,");
		cMess.replace("`/","sprites/");
		cMess.replace("`p",".png");
		return cMess;
	}//decompress(String eString)
	
	public void endTurn()
	{
		if(server)
		{
			try
			{
				if(turnList.size()>0)
				{
					turnUnit.setTurn(0);
					if(turnList.indexOf(turnUnit)>0)
					{
						turnUnit = (Unit)turnList.get(turnList.indexOf(turnUnit)-1);
					}else//if(turnList.indexOf(turnUnit)<(turnList.size()-1))
					{
						turnUnit = (Unit)turnList.get((turnList.size()-1));
					}//else -- if(turnList.indexOf(turnUnit)<(turnList.size()-1))
					turnUnit.setTurn(1);
				}else//if(turnList.size()>0)
				{
					turnUnit=null;
				}
			}catch(Exception e)
			{
				turnUnit=null;
			}//catch e
		}else//if(server)
		{
			String mess="";
			Unit u = null;
			for(int i=0;i<unitList.size();i++)
			{
				try
				{
					u = (Unit)unitList.get(i);
					if(u.getOwner().equalsIgnoreCase(userName))
					{
						mess = u.toString();
						i = unitList.size();
					}
				}catch(Exception e)
				{
					mess="!NO UNIT FROM PLAYER,";
				}//catch
			}//next i
			if(u==null)
			{
				mess="!NO UNIT FROM PLAYER,";
			}
			talker.setReturnMessage("!ENDTURN," + userName + "," + mess);
		}//else -- if(server)
	}//endTurn()
	
	public void addToTurnList(Unit unit)
	{
		if(turnList.size()>0)
		{
			for(int i=0;i<turnList.size();i++)
			{
				Unit u=null;
				u = (Unit)turnList.get(i);
				if(unit.getInitiative()<u.getInitiative())
				{
					turnList.add(i,unit);
					i=turnList.size();
				}else if(unit.getInitiative()==u.getInitiative())//if(unit.getInitiative()<u.getInitiative())
				{
					if(unit.getDex()<u.getDex())
					{
						turnList.add(i,unit);
						i=turnList.size();
					}else if(unit.getDex()==u.getDex())//if(unit.getDex()>u.getDex())
					{
						int r = generator.nextInt() % 2;
						if(r==0)
						{
							turnList.add(i,unit);
							i=turnList.size();
						}
					}else if(i==turnList.size()-1)//if(unit.getDex()>u.getDex()) -- if(unit.getDex()==u.getDex())
					{
						turnList.add(unit);
						i=turnList.size();
					}//else if(i==turnList.size()-1) -- if(unit.getDex()>u.getDex())
				}else if(i==turnList.size()-1)//else if(unit.getInitiative()==u.getInitiative()) -- if(unit.getInitiative()<u.getInitiative())
				{
					turnList.add(unit);
					i=turnList.size();
				}
			}//next i
		}else//if(turnList.size()>0)
		{
			turnList.add(unit);
		}//else -- if(turnList.size()>0)
	}//public void addToTurnList(Unit eUnit)
	
	public String checkUnitName(String name, Unit unit)
	{
		if(name.equals(""))
		{
			return checkUnitName("no name",unit);
		}//if(name.equals(""))
		int number=1;
		String tempName="";
		if(name.contains("#"))
		{
			String tempNumber="";
			int k = 0;
			while(name.charAt(k)!='#')
			{
				tempName=tempName + name.charAt(k);
				k=k+1;
			}//loop
			k=k+1;
			while(k<name.length())
			{
				tempNumber = tempNumber + name.charAt(k);
				k=k+1;
			}//loop
			try
			{
				number=Integer.parseInt(tempNumber);
				if(tempName.charAt(tempName.length()-1)==' ')
				{
					tempName = tempName.substring(0,tempName.length()-1);
				}//if(tempName.charAt(tempName.length()-1)==' ')
			}catch(Exception e)
			{
				if(tempName.length()>0)
				{
					return checkUnitName(tempName,unit);
				}else//if(tempName.length()>0)
				{
					return checkUnitName("Invalid name",unit);
				}//else -- if(tempName.length()>0)
			}//catch e
		}else//if(name.contains("#"))
		{
			tempName=name;
		}//else -- if(name.contains("#"))
		if(unitList.size()>0)
		{
			for(int i=0;i<unitList.size();i++)
			{
				Unit u = (Unit)unitList.get(i);
				if((u.getName().equals(name))&&(u!=unit))
				{
					number=number+1;
					return checkUnitName(tempName + " #" + number,unit);
				}//if(u.getName().equals(name))
			}//next i
		}//if(unitList.size()>0)
		return name;
	}//checkUnitName(String name, Unit unit)
	
	public String checkTerrainName(String name, Terrain terrain)
	{
		if(name.equals(""))
		{
			return checkTerrainName("no name",terrain);
		}//if(name.equals(""))
		int number=1;
		String tempName="";
		if(name.contains("#"))
		{
			String tempNumber="";
			int k = 0;
			while(name.charAt(k)!='#')
			{
				tempName=tempName + name.charAt(k);
				k=k+1;
			}//loop
			k=k+1;
			while(k<name.length())
			{
				tempNumber = tempNumber + name.charAt(k);
				k=k+1;
			}//loop
			try
			{
				number=Integer.parseInt(tempNumber);
				if(tempName.charAt(tempName.length()-1)==' ')
				{
					tempName = tempName.substring(0,tempName.length()-1);
				}//if(tempName.charAt(tempName.length()-1)==' ')
			}catch(Exception e)
			{
				if(tempName.length()>0)
				{
					return checkTerrainName(tempName,terrain);
				}else//if(tempName.length()>0)
				{
					return checkTerrainName("Invalid name",terrain);
				}//else -- if(tempName.length()>0)
			}//catch e
		}else//if(name.contains("#"))
		{
			tempName=name;
		}//else -- if(name.contains("#"))
		if(terrainList.size()>0)
		{
			for(int i=0;i<terrainList.size();i++)
			{
				Terrain t = (Terrain)terrainList.get(i);
				if((t.getName().equals(name))&&(t!=terrain))
				{
					number=number+1;
					return checkTerrainName(tempName + " #" + number,terrain);
				}//if(u.getName().equals(name))
			}//next i
		}//if(terrainList.size()>0)
		return name;
	}//checkTerrainName(String name, Terrain terrain)
	
	public String checkMarkerName(String name, Marker marker)
	{
		if(name.equals(""))
		{
			return checkMarkerName("no name",marker);
		}//if(name.equals(""))
		int number=1;
		String tempName="";
		if(name.contains("#"))
		{
			String tempNumber="";
			int k = 0;
			while(name.charAt(k)!='#')
			{
				tempName=tempName + name.charAt(k);
				k=k+1;
			}//loop
			k=k+1;
			while(k<name.length())
			{
				tempNumber = tempNumber + name.charAt(k);
				k=k+1;
			}//loop
			try
			{
				number=Integer.parseInt(tempNumber);
				if(tempName.charAt(tempName.length()-1)==' ')
				{
					tempName = tempName.substring(0,tempName.length()-1);
				}//if(tempName.charAt(tempName.length()-1)==' ')
			}catch(Exception e)
			{
				if(tempName.length()>0)
				{
					return checkMarkerName(tempName,marker);
				}else//if(tempName.length()>0)
				{
					return checkMarkerName("Invalid name",marker);
				}//else -- if(tempName.length()>0)
			}//catch e
		}else//if(name.contains("#"))
		{
			tempName=name;
		}//else -- if(name.contains("#"))
		if(markerList.size()>0)
		{
			for(int i=0;i<markerList.size();i++)
			{
				Marker m = (Marker)markerList.get(i);
				if((m.getName().equals(name))&&(m!=marker))
				{
					number=number+1;
					return checkMarkerName(tempName + " #" + number,marker);
				}//if(u.getName().equals(name))
			}//next i
		}//if(markerList.size()>0)
		return name;
	}//checkMarkerName(String name, Marker marker)
	
	public void removeUnit(Unit item)
	{
		try
		{
			Unit u = item;
			try
			{
				if(selectedPiece==u)
				{
					previouslySelectedPiece=selectedPiece;
					selectedPiece=null;
				}
			}catch(Exception e)
			{
				
			}//catch
			if(client)
			{
				try
				{
					if(myUnit==u)
					{
						myUnit=null;
					}
				}catch(Exception e)
				{
					
				}//catch
			}
			Vector v = (Vector)gameBoard.getSquareList().get(u.getRow());
			Square s = (Square)v.get(u.getColumn());
			s.getOccupees().remove(u);
			if(u.getMarkers().size()>0)
			{
				while(u.getMarkers().size()>0)
				{
					Marker m = (Marker)u.getMarkers().get(0);
					removeMarker(m);
				}//next i
			}
		}catch (Exception e)
		{
			
		}//catch
		//removeList.add(item);
		if(server)
		{
			turnList.remove(item);
		}
		if(turnUnit==item)
		{
			if(server)
			{
				endTurn();
			}else//if(server)
			{
				turnUnit=null;
			}//else -- if(server)
		}//if(myUnit==item)
		unitList.remove(item);
		gameBoard.setUnits(unitList);
	}//removeUnit
	
	public void addUnit(Unit item)
	{
		item.setName(checkUnitName(item.getName(),item));
		if(item.getOwner().equalsIgnoreCase(userName))
		{
			myUnit=item;
		}
		try
		{
			Unit p = (Unit)previouslySelectedPiece;
			/*if((formList.size()>0)&&(p.getName().equals(item.getName())))
			{
				for(int i=0;i<formList.size();i++)
				{
					try
					{
						StatsForm f = (StatsForm)formList.get(i);
						if(f.getUnit()==p)
						{
							//This will allow the unit to change in the stats form if the client 
							//has a stats form for this unit open.  Keep in mind, this is only the
							//case for one open stats form, and only if the client has the
							//corresponding unit selected.  This can be changed to be more robust
							//later, but for now, I am not too concerned with it.
							f.setUnit(item);
						}
					}catch(Exception e)
					{
						
					}
				}//next i
			}//if(formList.size()>0)*///I took it out for now b/c it caused issues
			if(item.getName().equals(p.getName())&&item.getOwner().equals(p.getOwner()))
			{
				selectedPiece=item;
				previouslySelectedPiece=null;
			}
		}catch(Exception e)
		{
			
		}
		Vector v = (Vector)gameBoard.getSquareList().get(item.getRow());
		Square s = (Square)v.get(item.getColumn());
		s.getOccupees().add(item);
		item.setX(s.getX());
		item.setY(s.getY());
		unitList.add(item);
		if(server)
		{
			addToTurnList(item);
		}
		if(item.getTurn()==1)
		{
			setTurnUnit(item);
		}//To fix the turn problem on the client
		for(int i=0;i<formList.size();i++)
		{
			try
			{
				AttackForm a = (AttackForm)formList.get(i);
				if(item.getName().equals(a.getUnit().getName()))
				{
					a.setUnit(item);
				}
			}catch(Exception e)
			{
				
			}//catch e
		}//next i
		gameBoard.setUnits(unitList);
	}//addUnit
	
	public void addTerrain(Terrain item)
	{
		item.setName(checkTerrainName(item.getName(),item));
		Terrain t = item;
		for(int i =0; i<t.getSizeX(); i++)
		{
			for(int j =0; j<t.getSizeY(); j++)
			{
				Vector v = (Vector)gameBoard.getSquareList().get(t.getRow()+j);
				Square s = (Square)v.get(t.getColumn()+i);
				s.getOccupees().add(t);
			}//next j
		}//next i
		terrainList.add(item);
		gameBoard.setTerrain(terrainList);
	}//addTerrain
	
	public void removeTerrain(Terrain item)
	{
		try
		{
			Terrain t = item;
			try
			{
				if(selectedPiece==t)
				{
					selectedPiece=null;
				}
			}catch(Exception e)
			{
				
			}//catch
			for(int i =0; i<t.getSizeX(); i++)
			{
				for(int j =0; j<t.getSizeY(); j++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(t.getRow()+j);
					Square s = (Square)v.get(t.getColumn()+i);
					s.getOccupees().remove(t);
				}//next j
			}//next i
		}catch (Exception e)
		{
			
		}//catch
		//removeList.add(item);
		terrainList.remove(item);
		gameBoard.setTerrain(terrainList);
	}//removeTerrain
	
	public void addMarker(Marker item)
	{
		item.setName(checkMarkerName(item.getName(),item));
		Marker m = item;
		Vector v = (Vector)gameBoard.getSquareList().get(m.getRow());
		Square s = (Square)v.get(m.getColumn());
		s.getOccupees().add(m);
		markerList.add(item);
		gameBoard.setMarkers(markerList);
	}//addMarker
	
	public void removeMarker(Marker item)
	{
		try
		{
			if(selectedPiece==item)
			{
				selectedPiece=null;
			}
		}catch(Exception e)
		{
			
		}//catch
		Vector v = (Vector)gameBoard.getSquareList().get(item.getRow());
		Square s = (Square)v.get(item.getColumn());
		s.getOccupees().remove(item);
		if(item.getUnit()!=null)
		{
			try
			{
				item.getUnit().getMarkers().remove(item);
			}catch (Exception e)
			{
				
			}//catch e
		}
		//removeList.add(item);
		markerList.remove(item);
		gameBoard.setMarkers(markerList);
	}//removeMarker
	
	public void addEffect(Effect item)
	{

		Effect e = item;
		Vector v = (Vector)gameBoard.getSquareList().get(e.getRow());
		Square s = (Square)v.get(e.getColumn());
		s.getOccupees().add(e);
		effectList.add(item);
		gameBoard.setEffects(effectList);
	}//addEffect
	
	public void removeEffect(Effect item)
	{

		try
		{
			Effect e = item;
			try
			{
				if(selectedPiece==e)
				{
					selectedPiece=null;
				}
			}catch(Exception e2)
			{
				
			}//catch
			Vector v = (Vector)gameBoard.getSquareList().get(e.getRow());
			Square s = (Square)v.get(e.getColumn());
			s.getOccupees().remove(e);
		}catch (Exception e)
		{
			
		}//catch
		//removeList.add(item);
		effectList.remove(item);
		gameBoard.setEffects(effectList);
	}//removeEffect
	
	public void addObject(Object item)
	{
		Object o = item;
		Vector v = (Vector)gameBoard.getSquareList().get(o.getRow());
		Square s = (Square)v.get(o.getColumn());
		s.getOccupees().add(o);
		objectList.add(item);
		gameBoard.setObjects(objectList);
	}//addObject
	
	public void removeObject(Object item)
	{
		try
		{
			Object o = item;
			try
			{
				if(selectedPiece==o)
				{
					selectedPiece=null;
				}
			}catch(Exception e)
			{
				
			}//catch
			Vector v = (Vector)gameBoard.getSquareList().get(o.getRow());
			Square s = (Square)v.get(o.getColumn());
			s.getOccupees().remove(o);
		}catch (Exception e)
		{
			
		}//catch
		//removeList.add(item);
		objectList.remove(item);
		gameBoard.setObjects(objectList);
	}//removeObject
	
	public void resizeMainWindow(Graphics2D g)
	{
		if(setup)
		{
			Vector temp = new Vector();
			for(int i=0;i<textBoxList.size();i++)
			{
				temp.add(textBoxList.get(i));
			}//next i
			textBoxList.clear();
			buttonList.clear();
			for(int i=0;i<10;i++)
			{
				textBoxList.add(new TextBox("",70,70+(30*i),g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
			}
			textBoxList.add(new TextBox("",(int)Math.floor(width/2)+20+g.getFontMetrics().stringWidth("Enter server's IP address:")+10,35,g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
			textBoxList.add(new TextBox("",(int)Math.floor(width/2)+20+g.getFontMetrics().stringWidth("Enter server's IP address:")+10,65,g.getFontMetrics().stringWidth("888.888.888.88888"),20,true,16));
			buttonList.add(new Button(this,(int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-10-20-20-10-g.getFontMetrics().stringWidth("Start server")),height-60,true,0,"Start server",Color.getHSBColor(57, 52, 84),g,true));
			buttonList.add(new Button(this,(int)Math.floor(width-g.getFontMetrics().stringWidth("Start client")-10-20),height-60,true,0,"Start client",Color.getHSBColor(57, 52, 84),g,true));
			for(int i=0;i<textBoxList.size();i++)
			{
				try
				{
					TextBox t = (TextBox)textBoxList.get(i);
					TextBox t2 = (TextBox)temp.get(i);
					t.setText(t2.getText());
				}catch(Exception e)
				{
					
				}
			}//next i
		}else//if(setup)
		{
			Board tempBoard = gameBoard;
			Vector tempUnits = (Vector) unitList.clone();
			Vector tempTerrain = (Vector) terrainList.clone();
			Vector tempMarkers = (Vector) markerList.clone();
			Vector tempObjects = (Vector) objectList.clone();
			Vector tempEffects = (Vector) effectList.clone();
			while(unitList.size()>0)
			{
				Unit u = (Unit)unitList.get(0);
				removeUnit(u);
			}//loop
			while(terrainList.size()>0)
			{
				Terrain t = (Terrain)terrainList.get(0);
				removeTerrain(t);
			}//loop
			while(markerList.size()>0)
			{
				Marker m = (Marker)markerList.get(0);
				removeMarker(m);
			}//loop
			while(objectList.size()>0)
			{
				Object o = (Object)objectList.get(0);
				removeObject(o);
			}//loop
			while(effectList.size()>0)
			{
				Effect e = (Effect)effectList.get(0);
				removeEffect(e);
			}//loop
			gameBoard=new Board(50,50,tempBoard.getBackground(),unitList,terrainList);
			int rows = gameBoard.getHeight();
			int cols = gameBoard.getWidth();
			int hdiv = height/squaresPerScreen;
			for(int i=0;i<rows;i++)
			{
				for(int j=0;j<cols;j++)
				{
					Vector v = (Vector)gameBoard.getSquareList().get(i);
					v.add(new Square("Square "+j+","+i,this,j*hdiv,i*hdiv,false,i,j));
				}//next j
			}//next i
			for(int i=0;i<tempUnits.size();i++)
			{
				Unit u = (Unit)tempUnits.get(i);
				Vector v = (Vector)gameBoard.getSquareList().get(u.getRow());
				Square s = (Square)v.get(u.getColumn());
				u.setX(s.getX());
				u.setY(s.getY());
				addUnit(u);
			}//next i
			for(int i=0;i<tempTerrain.size();i++)
			{
				Terrain t = (Terrain)tempTerrain.get(i);
				Vector v = (Vector)gameBoard.getSquareList().get(t.getRow());
				Square s = (Square)v.get(t.getColumn());
				t.setX(s.getX());
				t.setY(s.getY());
				addTerrain(t);
			}//next i
			for(int i=0;i<tempMarkers.size();i++)
			{
				Marker m = (Marker)tempMarkers.get(i);
				Vector v = (Vector)gameBoard.getSquareList().get(m.getRow());
				Square s = (Square)v.get(m.getColumn());
				m.setX(s.getX());
				m.setY(s.getY());
				addMarker(m);
			}//next i
			for(int i=0;i<tempObjects.size();i++)
			{
				Object o = (Object)tempObjects.get(i);
				Vector v = (Vector)gameBoard.getSquareList().get(o.getRow());
				Square s = (Square)v.get(o.getColumn());
				o.setX(s.getX());
				o.setY(s.getY());
				addObject(o);
			}//next i
			for(int i=0;i<tempEffects.size();i++)
			{
				Effect e = (Effect)tempEffects.get(i);
				Vector v = (Vector)gameBoard.getSquareList().get(e.getRow());
				Square s = (Square)v.get(e.getColumn());
				e.setX(s.getX());
				e.setY(s.getY());
				addEffect(e);
			}//next i
			if(server)
			{
				labelList.clear();
				textBoxList.clear();
				buttonList.clear();
				scrollBarList.clear();
				selectedTextBox=null;
				textBoxList.add(new TextBox("BoardName",(int)Math.floor(width*0.75)+20,450,200,20,true,0));
				labelList.add(new Label("lblTurnLabel","It is the following player's turn: ",(int)Math.floor(width*0.75)+20,20,100,20,true));
				try
				{
					labelList.add(new Label("lblTurnLabel2",turnUnit.getName(),(int)Math.floor(width*0.75)+20,40,100,20,true));
				}catch(Exception e)
				{
					labelList.add(new Label("lblTurnLabel2","ERROR",(int)Math.floor(width*0.75)+20,40,100,20,true));
				}//catch e
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,60,true,0,"Launch toolbox",Color.getHSBColor(57, 52, 84),g,true));
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,90,true,0,"End turn",Color.getHSBColor(57, 52, 84),g,true));
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,480,true,0,"Save board",Color.getHSBColor(57, 52, 84),g,true));
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,510,true,0,"Load board",Color.getHSBColor(57, 52, 84),g,true));
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,540,true,0,"Change back",Color.getHSBColor(57, 52, 84),g,true));
				showGridCheckBox = new CheckBox("chkShowGrid",(int)Math.floor(width*0.75)+20, 120, true, "Show grid");
				scrollBarList.add(new ScrollBar((int)Math.floor(width*0.75)-20,0,20,height,true,offsetY,hdiv*gameBoard.getHeight()));
				scrollBarList.add(new ScrollBar(0,height-20,20,(int)Math.floor(width*0.75)-20,false,offsetX,hdiv*gameBoard.getWidth()));
			}else//if(server)
			{
				labelList.clear();
				textBoxList.clear();
				buttonList.clear();
				scrollBarList.clear();
				selectedTextBox=null;
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,60,true,0,"New map view",Color.getHSBColor(57, 52, 84),g,true));
				buttonList.add(new Button(this,(int)Math.floor(width*0.75)+20,90,true,0,"End turn",Color.getHSBColor(57, 52, 84),g,true));
				labelList.add(new Label("lblTurnLabel","It is the following player's turn: ",(int)Math.floor(width*0.75)+20,20,100,20,true));
				showGridCheckBox = new CheckBox("chkShowGrid",(int)Math.floor(width*0.75)+20, 120, true, "Show grid");
				try
				{
					labelList.add(new Label("lblTurnLabel2",turnUnit.getName(),(int)Math.floor(width*0.75)+20,40,100,20,true));
				}catch(Exception e)
				{
					labelList.add(new Label("lblTurnLabel2","ERROR",(int)Math.floor(width*0.75)+20,40,100,20,true));
				}//catch e
				scrollBarList.clear();
				scrollBarList.add(new ScrollBar((int)Math.floor(width*0.75)-20,0,20,height,true,offsetY,hdiv*gameBoard.getHeight()));
				scrollBarList.add(new ScrollBar(0,height-20,20,(int)Math.floor(width*0.75)-20,false,offsetX,hdiv*gameBoard.getWidth()));
			}//else -- if(server)
		}//else -- if(setup)
	}//resizeMainWindow()
	
	public static void main(String argv[])
	{
		Start s = new Start();
		s.gameloop();
	}//main
	
	//GETTERS
	public Unit getTurnUnit()
	{
		return turnUnit;
	}
	public Vector getLabelList()
	{
		return labelList;
	}
	public String getUserName()
	{
		return userName;
	}
	public Vector getTurnList()
	{
		return turnList;
	}
	public Vector getUnitList()
	{
		return unitList;
	}
	public Vector getTerrainList()
	{
		return terrainList;
	}
	public Vector getMarkerList()
	{
		return markerList;
	}
	public Vector getEffectList()
	{
		return effectList;
	}
	public Vector getObjectList()
	{
		return objectList;
	}
	public Vector getTextBoxList()
	{
		return textBoxList;
	}
	public Vector getButtonList()
	{
		return buttonList;
	}
	public IntValue getOffsetX()
	{
		return offsetX;
	}
	public IntValue getOffsetY()
	{
		return offsetY;
	}
	public Board getBoard()
	{
		return gameBoard;
	}
	public boolean getServer()
	{
		return server;
	}
	public boolean getClient()
	{
		return client;
	}
	public Square getSelectedSquare()
	{
		return selectedSquare;
	}
	public FlatImage getSelectedPiece()
	{
		return selectedPiece;
	}
	public Unit getMyUnit()
	{
		return myUnit;
	}
	public int getSquaresPerScreen()
	{
		return squaresPerScreen;
	}
	public String getServerIP()
	{
		return serverIP;
	}
	public Client getTalker()
	{
		return talker;
	}
	public int getMouseX()
	{
		return mouseX;
	}
	public int getMouseY()
	{
		return mouseY;
	}
	public boolean getMouseLeft()
	{
		return mouseLeft;
	}
	public boolean getMouseRight()
	{
		return mouseRight;
	}
	public TextBox getSelectedTextBox()
	{
		return selectedTextBox;
	}
	public Vector getFormList()
	{
		return formList;
	}
	public boolean getSizingPiece()
	{
		return sizingPiece;
	}
	public boolean getMovingPiece()
	{
		return movingPiece;
	}
	public boolean getScrolling()
	{
		return scrolling;
	}
	public ScrollBar getSelectedScrollBar()
	{
		return selectedScrollBar;
	}
	public Vector getClientList()
	{
		return clientList;
	}
	public ToolBox getToolBox()
	{
		return toolBox;
	}
	public Random getGenerator()
	{
		return generator;
	}
	
	//SETTERS
	public void setToolBox(ToolBox eToolBox)
	{
		toolBox = eToolBox;
	}
	public void setUnitList(Vector eUnitList)
	{
		unitList=eUnitList;
	}
	public void setServer(boolean eServer)
	{
		server=eServer;
	}
	public void setClient(boolean eClient)
	{
		client=eClient;
	}
	public void setSetup(boolean eSetup)
	{
		setup=eSetup;
	}
	public void setSelectedPiece(FlatImage ePiece)
	{
		selectedPiece=ePiece;
	}
	public void setBoard(Board eBoard)
	{
		gameBoard=eBoard;
	}
	public void setBackName(String eName)
	{
		backName=eName;
	}
	public void setMouseLeft(boolean eSet)
	{
		mouseLeft=eSet;
	}
	public void setMouseX(int eX)
	{
		mouseX=eX;
	}
	public void setMouseY(int eY)
	{
		mouseY=eY;
	}
	public void setSelectedTextBox(TextBox t)
	{
		selectedTextBox=t;
	}
	public void setSelectedSquare(Square eSquare)
	{
		selectedSquare=eSquare;
	}
	public void setScrolling(boolean eScrolling)
	{
		scrolling = eScrolling;
	}
	public void setSelectedScrollBar(ScrollBar eBar)
	{
		selectedScrollBar=eBar;
	}
	public void setTurnUnit(Unit eUnit)
	{
		turnUnit=eUnit;
	}
	
	//KEY LISTENER
	private class KeyInputHandler extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
			{
				kBackspace = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_DELETE)
			{
				kDelete = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_1)
			{
				k1 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_2)
			{
				k2 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_3)
			{
				k3 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_4)
			{
				k4 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_5)
			{
				k5 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_6)
			{
				k6 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_7)
			{
				k7 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_8)
			{
				k8 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_9)
			{
				k9 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_0)
			{
				k0 = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_PERIOD)
			{
				kPeriod = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_A)
			{
				kA = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_B)
			{
				kB = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_C)
			{
				kC = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_D)
			{
				kD = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_E)
			{
				kE = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_F)
			{
				kF = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_G)
			{
				kG = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_H)
			{
				kH = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_I)
			{
				kI = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_J)
			{
				kJ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_K)
			{
				kK = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_L)
			{
				kL = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_M)
			{
				kM = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_N)
			{
				kN = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_O)
			{
				kO = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_P)
			{
				kP = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Q)
			{
				kQ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_R)
			{
				kR = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_S)
			{
				kS = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_T)
			{
				kT = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_U)
			{
				kU = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_V)
			{
				kV = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_W)
			{
				kW = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_X)
			{
				kX = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Y)
			{
				kY = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_Z)
			{
				kZ = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				kLeft = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				kRight = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				kUp = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN)
			{
				kDown = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				kSpace = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				kEnter = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				kShift = true;
			}
			if (e.getKeyCode() == KeyEvent.VK_CONTROL)
			{
				kControl = true;
			}
		}//keyPressed(KeyEvent e)
		
		public void keyReleased(KeyEvent e)
		{
			if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
			{
				kBackspace = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_DELETE)
			{
				kDelete = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_1)
			{
				k1 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_2)
			{
				k2 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_3)
			{
				k3 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_4)
			{
				k4 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_5)
			{
				k5 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_6)
			{
				k6 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_7)
			{
				k7 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_8)
			{
				k8 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_9)
			{
				k9 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_0)
			{
				k0 = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_PERIOD)
			{
				kPeriod = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_A)
			{
				kA = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_B)
			{
				kB = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_C)
			{
				kC = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_D)
			{
				kD = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_E)
			{
				kE = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_F)
			{
				kF = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_G)
			{
				kG = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_H)
			{
				kH = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_I)
			{
				kI = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_J)
			{
				kJ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_K)
			{
				kK = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_L)
			{
				kL = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_M)
			{
				kM = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_N)
			{
				kN = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_O)
			{
				kO = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_P)
			{
				kP = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Q)
			{
				kQ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_R)
			{
				kR = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_S)
			{
				kS = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_T)
			{
				kT = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_U)
			{
				kU = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_V)
			{
				kV = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_W)
			{
				kW = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_X)
			{
				kX = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Y)
			{
				kY = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_Z)
			{
				kZ = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_LEFT)
			{
				kLeft = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_RIGHT)
			{
				kRight = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_UP)
			{
				kUp = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_DOWN)
			{
				kDown = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE)
			{
				kSpace = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER)
			{
				kEnter = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_SHIFT)
			{
				kShift = false;
			}
			if (e.getKeyCode() == KeyEvent.VK_CONTROL)
			{
				kControl = false;
			}
		}//keyReleased(KeyEvent e)

		public void keyTyped(KeyEvent e)
		{
			if (e.getKeyChar() == 27)
			{
				//System.exit(0);
				selectedTextBox=null;
				for(int i=0;i<textBoxList.size();i++)
				{
					TextBox t = (TextBox) textBoxList.get(i);
					t.setSelected(false);
				}//next i
				selectedSquare=null;
				selectedPiece=null;
				previouslySelectedPiece=null;
				try
				{
					if(gameBoard.getSquareList().size()>0)
					{
						for(int i=0; i<gameBoard.getSquareList().size();i++)
						{
							Vector v = (Vector)gameBoard.getSquareList().get(i);
							if(v.size()>0)
							{
								for(int j=0; j<v.size();j++)
								{
									Square s = (Square)v.get(j);
									s.setSelected(false);
								}//next j
							}
						}//next i
					}
				}catch(Exception e2)
				{
					
				}//catch e2
			}//if (e.getKeyChar() == 27)
		}//keyTyped(KeyEvent e)
	}//class KeyInputHandler
	
	
	//MOUSE LISTENER
	private class MouseInputHandler implements MouseListener, MouseMotionListener
	{

		public void mouseClicked(MouseEvent e)
		{
			// Not used
		}

		public void mouseEntered(MouseEvent e)
		{
			// Not used
		}

		public void mouseExited(MouseEvent e)
		{
			// Not used
		}

		public void mousePressed(MouseEvent e)
		{
			if((e.getModifiers() & e.BUTTON3_MASK)== e.BUTTON3_MASK)
			{
				//This is the right-click event
				mouseRight = true;
				mouseX=e.getX();
				mouseY=e.getY();
			}else
			{
				//This is the left-click event
				mouseLeft = true;
				mouseX=e.getX();
				mouseY=e.getY();
				if(((System.currentTimeMillis()-clickTime)<300)&&(Math.abs(firstClickX-mouseX)<10)&&(Math.abs(firstClickY-mouseY)<10))
				{
					doubleClick=true;
				}else
				{
					firstClickX=mouseX;
					firstClickY=mouseY;
				}
				clickTime=System.currentTimeMillis();
			}
		}//mousePressed

		public void mouseReleased(MouseEvent e)
		{
			if((e.getModifiers() & e.BUTTON3_MASK)== e.BUTTON3_MASK)
			{
				//This is the right-click event
				mouseRight = false;
				mouseX=e.getX();
				mouseY=e.getY();
			}else
			{
				//This is the left-click event
				mouseLeft = false;
				mouseX=e.getX();
				mouseY=e.getY();
			}
			sizingPiece=false;
			movingPiece=false;
			//if(toolBox != null)
			//{
			//	toolBox.setDragging(false);
			//}
		}

		public void mouseDragged(MouseEvent e)
		{
			// Not used
		}

		public void mouseMoved(MouseEvent e)
		{
			// Not used
		}
		
	}//class MouseInputHandler
}//class Start